import { App } from "vue";
import { ElMessageBox, ElMessage } from "element-plus";
import dayjs from "dayjs";
import { nextId } from "@/util/nextId";
//操作确认弹窗
const defaultOption: any = {
  confirmButtonText: "确认",
  cancelButtonText: "取消",
  tip: "温馨提示!",
  type: "warning",
  content: "",
};
const confirmModel = (callBack: any, option: any) => {
  const { confirmButtonText, cancelButtonText, type, tip, content } =
    Object.assign(defaultOption, option);
  ElMessageBox.confirm(content, tip, {
    confirmButtonText,
    cancelButtonText,
    type,
  })
    .then(() => {
      callBack(true);
    })
    .catch(() => {
      callBack(false);
    });
};
const globalMessage = (
  message: string,
  type: "success" | "warning" | "info" | "error" = "info",
  showClose = true
) => {
  ElMessage({
    message,
    type,
    duration:2000,
    showClose,
  });
};
//判断数据类型
const globalCheckDataType = (val: any) => {
  let dataType = Object.prototype.toString.call(val);
  return dataType.slice(8, dataType.length - 1).toLowerCase();
};
//判断文件类型
const globalCheckFileType = (val: any) => {
  const types = [
    {
      value: 1,
      data: ["bmp", "jpg", "jpeg", "png", "gif", "webp", "svg"],
      zn: "图片",
    },
    {
      value: 2,
      data: [
        "avi",
        "wmv",
        "mpg",
        "mpeg",
        "mov",
        "rm",
        "ram",
        "swf",
        "flv",
        "mp4",
      ],
      zn: "视频",
    },
    {
      value: 3,
      data: ["txt", "doc", "docx"],
      zn: "文档",
    },
    {
      value: 4,
      data: [
        "mp3",
        "wav",
        "wma",
        "mp2",
        "flac",
        "midi",
        "ra",
        "ape",
        "aac",
        "cda",
        "m4a",
      ],
      zn: "音频",
    },
    {
      value: 5,
      data: ["zip", "7z"],
      zn: "压缩包",
    },
    {
      value: 6,
      data: [],
      zn: "文件夹",
    },
    {
      value: 7,
      data: ["pptx", "ppt", "pdf", "xlsx", "xls", "csv"], //目前已知，不在的也默认归类这里
      zn: "其他",
    },
  ];
  let obj: any = undefined,
    fileType = "",
    fileIndex = -1;
  fileIndex = val.lastIndexOf(".");
  fileType = val.slice(fileIndex + 1).toLowerCase();
  if (fileIndex == -1) {
    return {
      type: fileType,
      value: 6,
      zn: "文件夹",
    };
  } else {
    obj = types.find((item) => item.data.includes(fileType));
    return {
      type: fileType,
      value: obj ? obj.value : types.length,
      zn: obj ? obj.zn : "其他",
    };
  }
};
const formateDate = (val: any) => {
  return dayjs(val).format("YYYY-MM-DD HH:mm:ss");
};
export const globalPlgun = (app: App<Element> | undefined) => {
  app!.config.globalProperties.$gf = {
    gdt: globalCheckDataType,
    gft: globalCheckFileType,
    tip: confirmModel,
    formateDate: formateDate,
    msg: globalMessage,
  };
  app!.config.globalProperties.$nextId = nextId;
};
